import sys
import os

print "Building vector output files..."
print "time ./vecrep.sh /course/cs158/data/classif/stopWords.dat /course/cs158/data/classif/fullCollection.dat /course/cs158/data/classif/features1.dat vecoutput1.dat"
os.system("time ./vecrep.sh /course/cs158/data/classif/stopWords.dat /course/cs158/data/classif/fullCollection.dat /course/cs158/data/classif/features1.dat vecoutput1.dat")
print "time ./vecrep.sh /course/cs158/data/classif/stopWords.dat /course/cs158/data/classif/fullCollection.dat /course/cs158/data/classif/features2.dat vecoutput2.dat"
os.system("time ./vecrep.sh /course/cs158/data/classif/stopWords.dat /course/cs158/data/classif/fullCollection.dat /course/cs158/data/classif/features2.dat vecoutput2.dat")

print "Classifying documents..."

for comparedTo in ['test.dat', 'training1.dat', 'training2.dat']:
    for trainingSet in ['training1.dat', 'training2.dat']:
        for featureSet in ['features1.dat', 'features2.dat']:
            for algorithm in ['-mnb', '-r']:
                print 'cat /course/cs158/data/classif/' + comparedTo + ' | cut -d " " -f 1 > toClassify.dat'
                os.system('cat /course/cs158/data/classif/' + comparedTo + ' | cut -d " " -f 1 > toClassify.dat')
                
                vecOutput = 'vecoutput2.dat'
                if featureSet == 'features1.dat':
                    vecOutput = 'vecoutput1.dat'
                    
                cmd = 'time ./classify.sh ' + algorithm + ' /course/cs158/data/classif/' + featureSet + ' ' + vecOutput + ' /course/cs158/data/classif/' + trainingSet + ' toClassify.dat CLASSIFIER_OUTPUT.dat'
                print cmd
                os.system(cmd)
                
                # Number of docs to be classified:
                fout = os.popen('cat CLASSIFIER_OUTPUT.dat | wc -l')
                numDocsClassified = int(fout.read().rstrip('\n'))
                
                # Number of docs differing from provided classifications
                fout = os.popen("sdiff -B -b -s CLASSIFIER_OUTPUT.dat /course/cs158/data/classif/" + comparedTo + " | wc -l")
                numDocsDiffer = int(fout.read().rstrip('\n'))
                
                print "PERCENTAGE MATCHED = " + str((1.0 - (numDocsDiffer + 0.0)/(numDocsClassified + 0.0)) * 100)

